<template>
  <div>
    <!-- 
      inline:开启行内
     -->
    <el-form :inline="true" class="demo-form-inline" :model="form">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="form.category1Id" @change="hander1" :disabled="show">
          <el-option :label="c1.name" :value="c1.id" v-for="(c1, index) in list1" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="form.category2Id" @change="hander2" :disabled="show">
          <el-option :label="c2.name" :value="c2.id" v-for="(c2, index) in list2" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="form.category3Id" @change="hander3" :disabled="show">
          <el-option :label="c3.name" :value="c3.id" v-for="(c3, index) in list3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list1: [], //收集一级分类的名称
      list2: [],
      list3: [],
      form: {
        //收集一二三级的id
        category1Id: '',
        category2Id: '',
        category3Id: '',
      },
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    //获取一级分类的列表
    async getCategoryList() {
      let res = await this.$api.attribute.reqCategoryList()
      if (res.code === 200) {
        this.list1 = res.data
      }
    },
    //一级分类的select的change事件回调
    async hander1() {
      this.list2 = this.list3 = []
      this.form.category2Id = this.form.category3Id = ''
      this.$emit('getCategoryId', { category1Id: this.form.category1Id })
      let res = await this.$api.attribute.reqCategory2List({ category1Id: this.form.category1Id })
      if (res.code === 200) {
        this.list2 = res.data
      }
    },
    //二级分类的select的change事件回调
    async hander2() {
      this.list3 = []
      this.form.category3Id = ''
      this.$emit('getCategoryId', { category1Id: this.form.category1Id, category2Id: this.form.category2Id })
      let res = await this.$api.attribute.reqCategory3List({ category2Id: this.form.category2Id })
      if (res.code == 200) {
        this.list3 = res.data
      }
    },
    //三级分类的select的change事件回调
    async hander3() {
      this.$emit('getCategoryId', { category1Id: this.form.category1Id, category2Id: this.form.category2Id, category3Id: this.form.category3Id })
      // this.$emit('getCategoryId', this.form)
    },
  },
  props:['show']
}
</script>

<style>
</style>